package com.moyu.oasys.chat.controller;

import com.moyu.common.core.domain.Page;
import com.moyu.common.core.domain.R;
import com.moyu.oasys.chat.entity.Message;
import com.moyu.oasys.chat.entity.SessionInfo;
import com.moyu.oasys.chat.service.IChatService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Author 赵彦博
 * @Date 2024/12/3 20:32
 */
@RestController
@RequestMapping("/chats")
public class ChatController {

    private final IChatService chatService;

    public ChatController(IChatService messageService) {
        this.chatService = messageService;
    }

    @GetMapping("/listMessages")
    public R<Page<Message>> listMessages(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime start,
                                         @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                                         @RequestParam(defaultValue = "0") int pageNumber, @RequestParam(defaultValue = "10") int pageSize) {
        org.springframework.data.domain.Page<Message> messages = chatService.listMessage(start, end, pageNumber, pageSize);
        return R.ok(Page.of(messages.getContent(), messages.getTotalElements()));
    }

    @GetMapping("/session-info")
    public SessionInfo getSessionInfo() {
        // 示例中写死用户名和角色信息，实际需按真实业务逻辑从已认证的用户会话等地方获取相应的值
        String username = "example_user";
        String role = "user";
        return new SessionInfo(username, role);
    }

}
